# -*- coding: utf-8 -*-
# @Time    : 2020/8/11 10:20
# @Author  : ZSQ
# @Email   : zsq199170918@163.com
# @FileName: chq_dups.py
# @Software: PyCharm
import json
from redis import StrictRedis
from pymongo import MongoClient
from FDC_spider.utils.redis_pool import POOL

HOST = '192.168.5.236'
PORT = 27017


def get_chq_dups_eg_li():
    # mongo连接对象
    mongo_cli = MongoClient(host=HOST, port=PORT, )
    # redis连接对象
    redis_conn = StrictRedis(connection_pool=POOL)
    try:
        # mongo查询
        mongo_col = mongo_cli['zsq_chqfc']['estate']
        query_pipeline = [
            {'$group': {'_id': {'projectId': '$projectId', 'preSalePermit': '$preSalePermit', }, }},
            {'$group': {'_id': None, 'dups': {'$addToSet': '$_id'}}},
        ]
        estate_dups_li = list(mongo_col.aggregate(pipeline=query_pipeline, allowDiskUse=True))
        chq_dups_eg_li = estate_dups_li[0]['dups']
        # 写入redis
        redis_conn.delete('chq_dups_eg')
        for dups_dict in chq_dups_eg_li:
            redis_conn.sadd('chq_dups_eg', json.dumps(dups_dict))
    except:
        flag = False
    else:
        flag = True
    finally:
        # 关闭连接对象
        mongo_cli.close()
        redis_conn.close()
    return flag


def get_chq_dups_bd_li():
    # mongo连接对象
    mongo_cli = MongoClient(host=HOST, port=PORT, )
    # redis连接对象
    redis_conn = StrictRedis(connection_pool=POOL)
    try:
        # mongo查询
        mongo_col = mongo_cli['zsq_chqfc']['building']
        query_pipeline = [
            {'$group': {'_id': {'buildingId': '$buildingId', }, }},
            {'$group': {'_id': None, 'dups': {'$addToSet': '$_id.buildingId'}}},
        ]
        building_dups_li = list(mongo_col.aggregate(pipeline=query_pipeline, allowDiskUse=True))
        chq_dups_bd_li = building_dups_li[0]['dups']
        # 写入redis
        redis_conn.delete('chq_dups_bd')
        for dups_str in chq_dups_bd_li:
            redis_conn.sadd('chq_dups_bd', dups_str)
    except:
        flag = False
    else:
        flag = True
    finally:
        # 关闭连接对象
        mongo_cli.close()
        redis_conn.close()
    return flag


if __name__ == '__main__':
    # get_chq_dups_eg_li()
    # get_chq_dups_bd_li()
    pass
